using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIDesigner.ControlProviders.Bases;
using UIDesigner.FieldHandlers;
using System.ComponentModel;
using System.Drawing;
using DDW;
using System.Diagnostics;
using System.Windows.Forms;
using AFSharedLibrary.SharedControls;
using SharedClasses;
using UIDesigner.FieldHandlers.Bases;

namespace UIDesigner.ControlProviders
{
    [ControlProvider(ContainerControlOptions.CheckedListBox)]
	public class CheckedListBoxProvider : BaseControlProvider
    {
        public CheckedListBoxProvider(AFDesignerHost objHost) : base(objHost)
        {
        }

        public override void AddBindings(Control ctrlBinding, BaseNode objBase, string strBindingSourceProperty)
        {
        }

        public override Control AddControls(string strOption, TableLayoutPanel ctrlParent, BaseNode objBase, bool bDefault, bool bPreview, int nTabIndex, List<TableLayoutPanelCellPosition> rgTableLayoutPanelCellPositions, List<char> rgMnemonics)
        {
            string strName;
            string strLabelName;
            string strControlName;
            string strPrettyName;
            string strPrettyDescription;
            WindowsAPI.SIZEAPI sz;
            int hwnd;
            int hdc;
            CheckedListBox ctrlCheckedListBox;
            Label ctrlLabel;

            Debug.Assert(this.SupportsOption(strOption));

            strName = HandleName(objBase);
            strControlName = HandleName(GetControlPrefix(typeof(CheckedListBox)) + strName);
            strLabelName = HandleName(GetControlPrefix(typeof(Label)) + strName);

            if (bPreview)
            {
                ctrlCheckedListBox = new CheckedListBox();
                ctrlLabel = new Label();
            }
            else
            {
                ctrlCheckedListBox = (CheckedListBox)m_objHost.CreateComponent(typeof(CheckedListBox), strControlName);

                ctrlLabel = (Label)m_objHost.CreateComponent(typeof(Label), strLabelName);
            }

            sz = new WindowsAPI.SIZEAPI();
            hwnd = ctrlLabel.Handle.ToInt32();
            hdc = WindowsAPI.GetDC(hwnd);

            strPrettyName = GetPrettyName(objBase);
            strPrettyDescription = GetPrettyDescription(objBase);

            WindowsAPI.GetTextExtentPoint32(hdc, ref strPrettyName, strPrettyName.Length, ref sz);
            WindowsAPI.ReleaseDC(hwnd, hdc); 

            ctrlLabel.Name = strLabelName;
            ctrlLabel.Text = HandleMnemonics(strPrettyName, rgMnemonics); 
            ctrlLabel.Margin = new Padding(ctrlLabel.Margin.Left, ctrlLabel.Margin.Top + 3, ctrlLabel.Margin.Right, ctrlLabel.Margin.Bottom);
            ctrlLabel.Width = sz.cx + ctrlLabel.Margin.Left + ctrlLabel.Margin.Right;

            ctrlParent.Controls.Add(ctrlLabel, rgTableLayoutPanelCellPositions[0].Column, rgTableLayoutPanelCellPositions[0].Row);

            ctrlCheckedListBox.Name = strControlName;
            ctrlCheckedListBox.TabIndex = nTabIndex;

            ctrlCheckedListBox.AccessibleName = strPrettyName;
            ctrlCheckedListBox.AccessibleDescription = strPrettyDescription;

            ctrlParent.Controls.Add(ctrlCheckedListBox, rgTableLayoutPanelCellPositions[1].Column, rgTableLayoutPanelCellPositions[1].Row);

            return ctrlCheckedListBox;
        }

        public override Control AddControls(string strOption, IComponent ctrlParent, BaseNode objBase, bool bDefault, bool bPreview, int nTabIndex, List<Rectangle> rgLocationRects, List<char> rgMnemonics)
        {
            string strName;
            string strLabelName;
            string strControlName;
            string strPrettyName;
            string strPrettyDescription;

            Debug.Assert(this.SupportsOption(strOption));

            if (ctrlParent is DataGrid)
            {

            }
            else if (ctrlParent is ListView)
            {

            }
            else if (ctrlParent is TreeView)
            {

            }
            else if (ctrlParent is Control)
            {
                CheckedListBox ctrlCheckedListBox;
                Label ctrlLabel;
                Control ctrlParentControl = (Control) ctrlParent; 
                WindowsAPI.SIZEAPI sz;
                int hwnd;
                int hdc;

                strName = HandleName(objBase);
                strControlName = HandleName(GetControlPrefix(typeof(CheckedListBox)) + strName);
                strLabelName = HandleName(GetControlPrefix(typeof(Label)) + strName);

                if (bPreview)
                {
                    ctrlCheckedListBox = new CheckedListBox();
                    ctrlLabel = new Label();
                }
                else
                {
                    ctrlCheckedListBox = (CheckedListBox)m_objHost.CreateComponent(typeof(CheckedListBox), strControlName);

                    ctrlLabel = (Label)m_objHost.CreateComponent(typeof(Label), strLabelName);
                }

                sz = new WindowsAPI.SIZEAPI();
                hwnd = ctrlLabel.Handle.ToInt32();
                hdc = WindowsAPI.GetDC(hwnd);

                strPrettyName = GetPrettyName(objBase);
                strPrettyDescription = GetPrettyDescription(objBase);

                WindowsAPI.GetTextExtentPoint32(hdc, ref strPrettyName, strPrettyName.Length, ref sz);
                WindowsAPI.ReleaseDC(hwnd, hdc); 

                ctrlLabel.Location = new System.Drawing.Point(rgLocationRects[0].Left, rgLocationRects[0].Top);
                ctrlLabel.Name = strLabelName;
                ctrlLabel.Text = HandleMnemonics(strPrettyName, rgMnemonics); 
                ctrlLabel.Margin = new Padding(ctrlLabel.Margin.Left, ctrlLabel.Margin.Top + 3, ctrlLabel.Margin.Right, ctrlLabel.Margin.Bottom);
                ctrlLabel.Width = sz.cx + ctrlLabel.Margin.Left + ctrlLabel.Margin.Right;

                ctrlParentControl.Controls.Add(ctrlLabel);

                ctrlCheckedListBox.Location = new System.Drawing.Point(rgLocationRects[1].Left, rgLocationRects[1].Top);
                ctrlCheckedListBox.Name = strControlName;
                ctrlCheckedListBox.TabIndex = nTabIndex;

                ctrlCheckedListBox.AccessibleName = strPrettyName;
                ctrlCheckedListBox.AccessibleDescription = strPrettyDescription;

                ctrlParentControl.Controls.Add(ctrlCheckedListBox);

                return ctrlCheckedListBox;
            }

            return null;















        }

        public override void NegotiateLayout(string strOption, IComponent ctrlParent, BaseNode objBase, LayoutMode eLayoutMode, ref int nTabIndex, out LayoutStyle eLayoutStyle, out int nLabelAlignment, out Size szControlSize)
        {

            Debug.Assert(this.SupportsOption(strOption));

            eLayoutStyle = LayoutStyle.Unassigned;
            nLabelAlignment = -1;
            szControlSize = new Size();

            if (ctrlParent is DataGrid)
            {

            }
            else if (ctrlParent is ListView)
            {

            }
            else if (ctrlParent is TreeView)
            {

            }
            else if (ctrlParent is Control)
            {
                Label ctrlLabel = new Label();
                string strPrettyName = GetPrettyName(objBase);
                WindowsAPI.SIZEAPI sz = new WindowsAPI.SIZEAPI();
                int hwnd = ctrlLabel.Handle.ToInt32();
                int hdc = WindowsAPI.GetDC(hwnd);

                ctrlLabel.Margin = new Padding(ctrlLabel.Margin.Left, ctrlLabel.Margin.Top + 3, ctrlLabel.Margin.Right, ctrlLabel.Margin.Bottom);

                WindowsAPI.GetTextExtentPoint32(hdc, ref strPrettyName, strPrettyName.Length, ref sz);
                WindowsAPI.ReleaseDC(hwnd, hdc); 

                eLayoutStyle = LayoutStyle.VerticalDoubleCell;
                nLabelAlignment = sz.cy + ctrlLabel.Margin.Top + ctrlLabel.Margin.Bottom;

                szControlSize.Width = 200;
                szControlSize.Height = 100;
            }
        }
    }
}
